package com.example.exp05_client_aidl_remote_service;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.exp05_server_aidl_remote_service.MyAIDLService;

import java.text.DecimalFormat;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final String TAG = "Remote_ClientActivity";
    public  static DecimalFormat df = new DecimalFormat( "0.00"); //设置Double类型小数点后位数格式
    private EditText editText1;
    private EditText editText2;
    private Intent bindIntent;
    private MyAIDLService myAIDLService;
    MyAIDLService.Stub myBinder;

    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            myAIDLService = MyAIDLService.Stub.asInterface(iBinder);

            try {
//                Random random = new Random();
//                double numb1 = random.nextDouble();
//                double numb2 = random.nextDouble();
                double numb1 = Double.valueOf(editText1.getText().toString());
                double numb2 = Double.valueOf(editText2.getText().toString());
                double plusResult = myAIDLService.plus(numb1,numb2);
                double subResult = myAIDLService.sub(numb1,numb2);
                double multiplyResult = myAIDLService.multiply(numb1,numb2);

                Toast.makeText(MainActivity.this,
                        "Numb1: "+df.format(numb1)+" Numb2: "+df.format(numb2)+
                                "\n返回结果分别为："+df.format(plusResult)+","+df.format(subResult)+","+df.format(multiplyResult),
                        Toast.LENGTH_SHORT).show();
            }catch (RemoteException e){
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Toast.makeText(MainActivity.this,
                    "onServiceDisconnected() executed",
                    Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText1 = findViewById(R.id.numb1);
        editText2 = findViewById(R.id.numb2);

        findViewById(R.id.bindService).setOnClickListener(this);
        findViewById(R.id.unbindService).setOnClickListener(this);

        bindIntent = new Intent();
        bindIntent.setComponent(new ComponentName("com.example.exp05_server_aidl_remote_service",
                "com.example.exp05_server_aidl_remote_service.MyService"));
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.bindService:
                // 对于远程调用而言intent的调用方式要变
                bindService(bindIntent,connection,BIND_AUTO_CREATE);
                break;
            case R.id.unbindService:
                unbindService(connection);
                break;
            default:
                break;
        }
    }
}